#include "../lv_project_inc/lvgl_databar.h"

//湿度
void lvgl_layout_hum(lv_obj_t *obj)
{
    lv_obj_set_size(obj,50,300);
    lv_obj_align(obj,LV_ALIGN_TOP_RIGHT,-50,40);
    lv_obj_set_style_pad_all(obj,0,0);
    lv_obj_set_style_border_width(obj,0,0);
    lv_obj_set_style_bg_color(obj,lv_color_hex(0x4F4F4F),0);
    
    bar_hum = lv_bar_create(obj);
    lv_obj_set_size(bar_hum,20,200);
    lv_obj_align(bar_hum,LV_ALIGN_CENTER,0,-10);
    lv_obj_set_style_bg_color(bar_hum,lv_color_hex(0xffffff),0);
    lv_obj_set_style_bg_color(bar_hum,lv_color_hex(0x00BFFF),LV_PART_INDICATOR);
    lv_obj_set_style_bg_opa(bar_hum,LV_OPA_COVER,0);
    lv_bar_set_range(bar_hum,0,100);

    lv_img_t *img_hum = lv_img_create(obj);
    LV_IMG_DECLARE(hum);
    lv_img_set_src(img_hum,&hum);
    lv_obj_align(img_hum,LV_ALIGN_BOTTOM_MID,0,0);

    label_hum = lv_label_create(obj);
    lv_obj_align_to(label_hum,bar_hum,LV_ALIGN_OUT_TOP_MID,-6,-10);
    lv_obj_set_style_text_font(label_hum,&lv_font_montserrat_20,0);
    lv_obj_set_style_text_color(label_hum,lv_color_hex(0xffffff),0);
}

//温度
void lvgl_layout_temp(lv_obj_t *obj)
{
    lv_obj_set_size(obj,50,300);
    lv_obj_align(obj,LV_ALIGN_TOP_RIGHT,-150,40);
    lv_obj_set_style_pad_all(obj,0,0);
    lv_obj_set_style_border_width(obj,0,0);
    lv_obj_set_style_bg_color(obj,lv_color_hex(0x4F4F4F),0);
    
    bar_temp = lv_bar_create(obj);
    lv_obj_set_size(bar_temp,20,200);
    lv_obj_align(bar_temp,LV_ALIGN_CENTER,0,-10);
    lv_obj_set_style_bg_color(bar_temp,lv_color_hex(0xffffff),0);
    lv_obj_set_style_bg_color(bar_temp,lv_color_hex(0xFF6A6A),LV_PART_INDICATOR);
    lv_obj_set_style_bg_opa(bar_temp,LV_OPA_COVER,0);
    lv_bar_set_range(bar_temp,0,50);

    lv_img_t *img_temp = lv_img_create(obj);
    LV_IMG_DECLARE(temp);
    lv_img_set_src(img_temp,&temp);
    lv_obj_align(img_temp,LV_ALIGN_BOTTOM_MID,0,0);

    label_temp = lv_label_create(obj);
    lv_obj_align_to(label_temp,bar_temp,LV_ALIGN_OUT_TOP_MID,-6,-10);
    lv_obj_set_style_text_font(label_temp,&lv_font_montserrat_18,0);
    lv_obj_set_style_text_color(label_temp,lv_color_hex(0xffffff),0);
}

//空气质量
void lvgl_layout_airq(lv_obj_t *obj)
{
    lv_obj_set_size(obj,200,200);
    lv_obj_align(obj,LV_ALIGN_CENTER,75,0);
    lv_obj_set_style_pad_all(obj,0,0);
    lv_obj_set_style_border_width(obj,0,0);
    lv_obj_set_style_bg_color(obj,lv_color_hex(0x4F4F4F),0);

    arc_airq = lv_arc_create(obj);
    lv_obj_align(arc_airq,LV_ALIGN_CENTER,0,0);
    lv_obj_remove_style(arc_airq,NULL,LV_PART_KNOB);
    lv_obj_clear_flag(arc_airq,LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_style_arc_color(arc_airq,lv_color_hex(0xffffff),0);
    lv_obj_set_style_arc_color(arc_airq,lv_color_hex(0xADFF2F),LV_PART_INDICATOR);
    lv_obj_set_style_arc_width(arc_airq,20,0);
    lv_obj_set_style_arc_width(arc_airq,20,LV_PART_INDICATOR);
    lv_obj_set_size(arc_airq,175,175);
    lv_arc_set_range(arc_airq,0,250);

    label_airq = lv_label_create(obj);
    lv_obj_align(label_airq,LV_ALIGN_CENTER,0,0);
    lv_obj_set_style_text_font(label_airq,&lv_font_montserrat_18,0);
    lv_obj_set_style_text_color(label_airq,lv_color_hex(0xffffff),0);

    lv_img_t *img_airq = lv_img_create(obj);
    LV_IMG_DECLARE(airq);
    lv_img_set_src(img_airq,&airq);
    lv_obj_align(img_airq,LV_ALIGN_BOTTOM_MID,0,0);

    pthread_t thread_airq;
    pthread_create(&thread_airq,NULL,lvgl_simulate_airq_data,NULL);
    
    pthread_t thread_data;
    pthread_create(&thread_data,NULL,lvgl_simulate_data,NULL);
}

//更新空气质量数据
void lvgl_airq_update()
{
    lvgl_lock();
    lv_arc_set_value(arc_airq,value_arc_airq);
    lvgl_unlock();

    memset(text_label_airq,0,sizeof(text_label_airq));
    sprintf(text_label_airq,"%d",value_arc_airq);
    lvgl_lock();
    lv_label_set_text(label_airq,text_label_airq);
    lvgl_unlock();
}

//更新温湿度数据
void lvgl_data_update()
{
    srand(time(NULL));

    //随机湿度
    value_bar_hum = rand() % 100;
    lvgl_lock();
    lv_bar_set_value(bar_hum,value_bar_hum,LV_ANIM_ON);
    lvgl_unlock();

    memset(text_label_hum,0,sizeof(text_label_hum));
    sprintf(text_label_hum,"%d%%",value_bar_hum);
    lvgl_lock();
    lv_label_set_text(label_hum,text_label_hum);
    lvgl_unlock();

    //随机温度
    value_bar_temp = rand() % 50;
    lvgl_lock();
    lv_bar_set_value(bar_temp,value_bar_temp,LV_ANIM_ON);
    lvgl_unlock();

    memset(text_label_temp,0,sizeof(text_label_hum));
    sprintf(text_label_temp,"%d°C",value_bar_temp);
    lvgl_lock();
    lv_label_set_text(label_temp,text_label_temp);
    lvgl_unlock();
}

void* lvgl_simulate_airq_data(void *arg)
{
    srand(time(NULL));

    while(1)
    {
        //空气质量
        value_arc_airq += rand() % 10;

        lvgl_airq_update();

        if(value_arc_airq >= 250)
        {
            state_smoke = true;
            state_beep = true;
            lvgl_secure_update(state_smoke);

            secure_beep_danger();

            sleep(10);

            value_arc_airq = 0;
            
            lvgl_airq_update();
            
            state_smoke = false;
            state_beep = false;
            lvgl_secure_update(state_smoke);
        }

        sleep(1);
    }
}

void* lvgl_simulate_data(void *arg)
{
    while(1)
    {
        lvgl_data_update();
        sleep(1);
    }
}